﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace IntelligentCMS
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void theLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {

            DataTable logininfo = CMSDataLayer.GetHomepageUserIDFromUsernamePassword(theLogin.UserName.Trim(), theLogin.Password.Trim());


            if (logininfo.Rows.Count <= 0)
            {
                return;
            }

            homepage = common.FromBase64String(logininfo.Rows[0]["Homepage"].ToString());

            Session["UserID"] = long.Parse(logininfo.Rows[0]["ID"].ToString());
            Session["Username"] = theLogin.UserName.Trim();


            Response.Redirect(homepage);



        }

        protected void theCreateUserWizard_ContinueButtonClick(object sender, EventArgs e)
        {
            DataTable logininfo = CMSDataLayer.GetHomepageUserIDFromUsername(theCreateUserWizard.UserName.Trim());

            if (logininfo.Rows.Count <= 0)
            {
                return;
            }



            homepage = common.FromBase64String(logininfo.Rows[0]["Homepage"].ToString());

            Session["UserID"] = long.Parse(logininfo.Rows[0]["ID"].ToString());
            Session["Username"] = theCreateUserWizard.UserName.Trim();

            Response.Redirect(homepage);

        }
        string homepage = string.Empty;
        protected void theCreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            
            

        }

        protected void theCreateUserWizard_CreatedUser(object sender, EventArgs e)
        {
            homepage = "welcome.aspx";

            CMSDataLayer.CreateNewUser(theCreateUserWizard.UserName.Trim(), theCreateUserWizard.Password.Trim(), theCreateUserWizard.Email.Trim(), theCreateUserWizard.Question.Trim(), theCreateUserWizard.Answer.Trim(), homepage);


        }
    }
}
